package com.duoduo.sanguoshanews;

import com.duoduo.adapter.SanGuoShaAdapter;
import com.duoduo.bean.NewsTitle;
import com.duoduo.data.WebData;
import com.duoduo.db.DBHelper;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;

import java.io.IOException;
import java.util.List;
import com.duoduo.widget.PullToRefreshListView;
import com.duoduo.widget.PullToRefreshListView.OnRefreshListener;

public class SanGuoShaNewsActivity extends Activity {
	private PullToRefreshListView listView;
	private Handler handler;
	private DBHelper dbHelper;
	private SanGuoShaAdapter sanGuoShaAdapter;
	private List<NewsTitle> data;
	private String newUrl;
	private String title;
	private ProgressDialog progressDialog;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_san_guo_sha_news);
		handler = new Handler();
		dbHelper = new DBHelper(this);
		listView = (PullToRefreshListView) SanGuoShaNewsActivity.this
				.findViewById(R.id.news_list_view);

		sanGuoShaAdapter = new SanGuoShaAdapter(this, dbHelper.getAllNews());

		if (sanGuoShaAdapter.getCount() == 0) {
			updateData(true);
		}
		listView.setAdapter(sanGuoShaAdapter);

		listView.setOnRefreshListener(new OnRefreshListener() {
			public void onRefresh() {
				updateData(false);
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Cursor cursor = (SQLiteCursor) parent
						.getItemAtPosition(position);
				newUrl = cursor.getString(cursor
						.getColumnIndex(DBHelper.NEWS_COLUMN_URL));
				title = cursor.getString(cursor
						.getColumnIndex(DBHelper.NEWS_COLUMN_CONTENT));
				Intent intent = new Intent(SanGuoShaNewsActivity.this,
						NewDetailActivity.class);
				intent.putExtra("url", newUrl);
				intent.putExtra("title", title);
				startActivity(intent);

			}
		});

	}

	protected void onDestroy() {
		dbHelper.deleteOldNewsDetail();
		dbHelper.close();
		super.onDestroy();
	}

	public void updateData(final boolean isTitleTableEmpty) {
		if (isTitleTableEmpty) {
			progressDialog = ProgressDialog.show(this, "载入中", "新闻加载中，请稍后...");
		}
		Thread thread = new Thread() {
			public void run() {
				try {

					data = WebData.getNewsFromWeb(SanGuoShaNewsActivity.this);
				} catch (IOException e) {
					e.printStackTrace();
				}
				dbHelper.saveAllNews(data);
				handler.post(new Runnable() {
					public void run() {
						sanGuoShaAdapter.changeCursor(dbHelper.getAllNews());
						listView.onRefreshComplete("更新完成");
						if (progressDialog != null && isTitleTableEmpty) {
							progressDialog.dismiss();
						}
						Toast.makeText(SanGuoShaNewsActivity.this, "更新完成",
								Toast.LENGTH_SHORT).show();
					}
				});
			}
		};
		thread.start();

	}

}
